package com.dc.di.sf.dt;

import org.apache.axiom.om.OMElement;
import org.apache.commons.codec.binary.Base64;

import com.dc.di.sf.utils.OMUtils;

public class DataUnit implements DTObject<DataUnit> {

	public static final String TYPE_STRING = "string";
	public static final String TYPE_DATETIME = "datetime";
	public static final String TYPE_DECIMAL = "decimal";
	public static final String TYPE_BINARY = "binary";
	public static final String TYPE_INTEGER = "integer";
	public static final String TYPE_NUMBER = "number";

	public static final String TAG_DATAUNIT = "DataUnit";
	public static final String TAG_IDNAME = "IDName";
	public static final String TAG_DISPLAYNAME = "DisplayName";
	public static final String TAG_DEFINITION = "Definition";
	public static final String TAG_COMMENTS = "Comments";
	public static final String TAG_DATATYPE = "Datatype";
	public static final String TAG_MAXIMUMSIZE = "MaximumSize";
	public static final String TAG_MINIMUMSIZE = "MinimumSize";
	public static final String TAG_SCALE = "Scale";

	private String idName;
	private String displayName;
	private Object definition;
	private String comments;
	private String dataType;
	private Integer maximumSize;
	private Integer minimumSize;
	private Integer scale;

	public DataUnit() {

	}

	public DataUnit(OMElement xml) {
		this();
		fromXML(xml);
	}

	/**
	 * @return the idName
	 */
	public String getIdName() {
		return idName;
	}

	/**
	 * @param idName
	 *            the idName to set
	 */
	public void setIdName(String idName) {
		this.idName = idName;
	}

	/**
	 * @return the displayName
	 */
	public String getDisplayName() {
		return displayName;
	}

	/**
	 * @param displayName
	 *            the displayName to set
	 */
	public void setDisplayName(String displayName) {
		this.displayName = displayName;
	}

	/**
	 * @return the definition
	 */
	public Object getDefinition() {
		return definition;
	}

	/**
	 * @param definition
	 *            the definition to set
	 */
	public void setDefinition(Object definition) {
		this.definition = definition;
	}

	/**
	 * @return the comments
	 */
	public String getComments() {
		return comments;
	}

	/**
	 * @param comments
	 *            the comments to set
	 */
	public void setComments(String comments) {
		this.comments = comments;
	}

	/**
	 * @return the dataType
	 */
	public String getDataType() {
		return dataType;
	}

	/**
	 * @param dataType
	 *            the dataType to set
	 */
	public void setDataType(String dataType) {
		this.dataType = dataType;
	}

	/**
	 * @return the maximumSize
	 */
	public int getMaximumSize() {
		return maximumSize;
	}

	/**
	 * @param maximumSize
	 *            the maximumSize to set
	 */
	public void setMaximumSize(int maximumSize) {
		this.maximumSize = maximumSize;
	}

	/**
	 * @return the minimumSize
	 */
	public int getMinimumSize() {
		return minimumSize;
	}

	/**
	 * @param minimumSize
	 *            the minimumSize to set
	 */
	public void setMinimumSize(int minimumSize) {
		this.minimumSize = minimumSize;
	}

	/**
	 * @return the scale
	 */
	public int getScale() {
		return scale;
	}

	/**
	 * @param scale
	 *            the scale to set
	 */
	public void setScale(int scale) {
		this.scale = scale;
	}

	public OMElement toXML() {
		OMElement unit = OMUtils.createOMElement(TAG_DATAUNIT);
		unit.addChild(OMUtils.createOMElementWithText(TAG_IDNAME, idName));
		unit.addChild(OMUtils.createOMElementWithText(TAG_DISPLAYNAME, displayName));
		if (definition != null)
			unit.addChild(OMUtils.createOMElementWithText(TAG_DEFINITION, definition.toString()));
		unit.addChild(OMUtils.createOMElementWithText(TAG_COMMENTS, comments));
		unit.addChild(OMUtils.createOMElementWithText(TAG_DATATYPE, dataType));
		if (maximumSize != null)
			unit.addChild(OMUtils.createOMElementWithText(TAG_MAXIMUMSIZE, maximumSize.toString()));
		if (minimumSize != null)
			unit.addChild(OMUtils.createOMElementWithText(TAG_MINIMUMSIZE, minimumSize.toString()));
		if (scale != null)
			unit.addChild(OMUtils.createOMElementWithText(TAG_SCALE, scale.toString()));
		return unit;
	}

	public DataUnit fromXML(OMElement xml) {
		if (xml != null) {
			idName = OMUtils.getTextOfChild(xml, TAG_IDNAME);
			displayName = OMUtils.getTextOfChild(xml, TAG_DISPLAYNAME);
			definition = OMUtils.getTextOfChild(xml, TAG_DEFINITION);
			comments = OMUtils.getTextOfChild(xml, TAG_COMMENTS);
			dataType = OMUtils.getTextOfChild(xml, TAG_DATATYPE);
			// maximumSize = new Integer(OMUtils.getTextOfChild(xml,
			// TAG_MAXIMUMSIZE));
			// minimumSize = new Integer(OMUtils.getTextOfChild(xml,
			// TAG_MINIMUMSIZE));
			// scale = new Integer(OMUtils.getTextOfChild(xml, TAG_SCALE));
		}
		return this;
	}

	/* (non-Javadoc)
	 * @see com.dc.di.sf.dt.DTObject#toXMLString()
	 */
	public String toXMLString() {
		StringBuilder sb = new StringBuilder();
		sb.append(OMUtils.generateOMElementBeginString(TAG_DATAUNIT));
		sb.append(OMUtils.generateOMElementString(TAG_IDNAME, idName));
		sb.append(OMUtils.generateOMElementString(TAG_DISPLAYNAME, displayName));
		if (definition != null)
			sb.append(OMUtils.generateOMElementString(TAG_DEFINITION, definition.toString()));
		sb.append(OMUtils.generateOMElementString(TAG_COMMENTS, comments));
		sb.append(OMUtils.generateOMElementString(TAG_DATATYPE, dataType));
		if (maximumSize != null)
			sb.append(OMUtils.generateOMElementString(TAG_MAXIMUMSIZE, maximumSize.toString()));
		if (minimumSize != null)
			sb.append(OMUtils.generateOMElementString(TAG_MINIMUMSIZE, minimumSize.toString()));
		if (scale != null)
			sb.append(OMUtils.generateOMElementString(TAG_SCALE, scale.toString()));
		sb.append(OMUtils.generateOMElementEndString(TAG_DATAUNIT));
		return sb.toString();
	}

}
